/*
 * @Author: fhw
 * @Date: 2022-05-08 17:48:50
 * @Description: 文章仓库
 */
import { getModule, Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
import store from '@/store'
import { Options } from './basicData'
import { isNil } from 'ramda'
interface CategoryIsTag {
	id: number
	name: string
}
@Module({ namespaced: true, store, name: 'articleData', dynamic: true })
export class ArticleData extends VuexModule {
	public articleCategory: Options[] = []
	public articleTag: Options[] = []
	public articleLately: Options[] = []
	public articleHot: Options[] = []
	// 文章分类
	@Action
	public async getArticleCategory() {
		const { success, data } = await API.blog.articleCategory.getArticleCategory.request({ pageSize: 99999999 })
		if (success && !isNil(data)) {
			this.GETARTICLECATEGORY(data.data ?? [])
		}
	}
	@Mutation
	public async GETARTICLECATEGORY(data: CategoryIsTag[]) {
		this.articleCategory = data.map((item) => {
			return {
				label: item.name ?? '',
				value: item.id + '',
			}
		})
	}
	// 文章标签
	@Action
	public async getArticleTag() {
		const { success, data } = await API.blog.articleTag.getArticleTag.request({ pageSize: 99999999 })
		if (success && !isNil(data)) {
			this.GETARTICLETAG(data.data ?? [])
		}
	}
	@Mutation
	public async GETARTICLETAG(data: CategoryIsTag[]) {
		this.articleTag = data.map((item) => {
			return {
				label: item.name ?? '',
				value: item.id + '',
			}
		})
	}
	@Action
	public async getArticleLately(params = {}) {
		const { success, data } = await API.blog.article.getArticleLately.request(params)
		if (success && !isNil(data)) {
			this.GETARTICLELATELY(data ?? [])
		}
	}
	@Mutation
	public async GETARTICLELATELY(data: any[]) {
		this.articleLately = data.map((item) => {
			return {
				label: item.title ?? '',
				value: item.id,
			}
		})
	}
	@Action
	public async getArticleHot(params = {}) {
		const { success, data } = await API.blog.article.getArticleHot.request(params)
		if (success && !isNil(data)) {
			this.GETARTICLEHOT(data ?? [])
		}
	}
	@Mutation
	public async GETARTICLEHOT(data: any[]) {
		this.articleHot = data.map((item) => {
			return {
				label: item.title,
				value: item.id,
			}
		})
	}
}

export const ArticleModule = getModule(ArticleData)
